﻿using System.Windows;
using System.Windows.Data;
using System.Windows.Forms;
using Xceed.Wpf.Toolkit.PropertyGrid;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
using Binding = System.Windows.Data.Binding;

namespace ABCat.Core.Editors
{
	/// <summary>
	/// Interaction logic for FolderPathEditor.xaml
	/// </summary>
	public partial class FolderPathEditorUc
	{


		public string FolderPath
		{
			get { return (string)GetValue(FolderPathProperty); }
			set { SetValue(FolderPathProperty, value); }
		}

		// Using a DependencyProperty as the backing store for FolderPath.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty FolderPathProperty =
						DependencyProperty.Register("FolderPath", typeof(string), typeof(FolderPathEditorUc), new PropertyMetadata(null));



		public FolderPathEditorUc()
		{
			InitializeComponent();
		}

		private void SelectFolderBtn_OnClick(object sender, RoutedEventArgs e)
		{
			var fbd = new FolderBrowserDialog();
			fbd.SelectedPath = FolderPath;
			fbd.ShowNewFolderButton = true;

			if (fbd.ShowDialog() == DialogResult.OK)
			{
				FolderPath = fbd.SelectedPath;
			}
		}
	}

	public class FolderPathEditor : ITypeEditor
	{
		public FrameworkElement ResolveEditor(PropertyItem propertyItem)
		{
			var fpe = new FolderPathEditorUc();
			var binding = new Binding("Value");
			binding.Source = propertyItem;
			binding.ValidatesOnExceptions = true;
			binding.ValidatesOnDataErrors = true;
			binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
			BindingOperations.SetBinding(fpe, FolderPathEditorUc.FolderPathProperty, binding);
			return fpe;
		}
	}
}
